package com.sunchangming.rpcgen;

public class TypeBoolean extends Type {

	@Override
	public String getName(Variable variable) {
		return "boolean";
	}

	@Override
	public String getMarshalStr(Variable v, String osname) {
		return String.format("%s.writeBoolean(%s)", osname, v.getName());
	}

	@Override
	public String getUnmarshalStr(Variable v, String osname, boolean useThis) {
		return String.format((useThis ? "this." : "") + "%s=%s.readBoolean()", v.getName(), osname);
	}

	@Override
	public String getBoxName() {
		return "Boolean";
	}

	@Override
	public String getDefaultValue(Variable variable) {
		final String userstr = variable.defaultvalue;
		if (userstr != null && !userstr.isEmpty()) {
			if (userstr.equals("true"))
				return "true";
			else if (userstr.equals("false"))
				return "false";
			else
				throw new RuntimeException("错误的默认值" + userstr);
		}
		return "false";
	}

	@Override
	public String getTestEqualsStr(Variable variable, String string) {
		return "this." + variable.getName() + "== " + string + "." + variable.getName();
	}

	@Override
	public String getHashCodeStr(Variable variable) {
		return "this." + variable.getName() + "?1:0";
	}

	@Override
	public String getFromSqlStr(Variable variable, String string) {
		return String.format("this.%s=%s.getBoolean(\"%s\")", variable.getName(), string, variable.getFrom());
	}

}
